const express = require('express')
const router = express.Router()
const fs = require("fs")
const db = require("../data/db")
const jwt = require('jsonwebtoken')



//登录
var dl = async(req, res) => {
    var { username, password } = req.body
    var result = await getuserbynameandpwd(username, password)
    if (result.length === 0) {
        res.json({
            code: -1,
            message: "用户名或密码错误"
        })
        return
    } else {
        var token = jwt.sign({...result[0] }, "1", {
            expiresIn: "24h"
        })
        console.log(token);

        res.json({
            code: 200,
            message: "登录",
            token,
        })
    }
    console.log(result);
}

var getuserbynameandpwd = (username, password) => {
    var sql = `select * from user where username=? and password=?`
    return new Promise((resolve, reject) => {
        db.query(sql, [username, password], (err, data) => {
            if (err) {
                reject
                    (err);
            }
            resolve(data)
        })
    })
}

//注册
var login = (req, res) => {
    var sql = "INSERT INTO `sys`.`user` (`Username`, `password`) VALUES (?, ?)"
    db.query(sql, [req.body.username, req.body.password], (err, data) => {
        if (err) {
            res.send(err);
        }
        res.send(data)

    })
}

//列表
var list = (req, res) => {
    var sql = "SELECT * FROM `sys`.`img` LIMIT 0,1000"
    db.query(sql, (err, data) => {
        if (err) {
            res.send(err);
        }
        res.send(data)

    })
}


//添加
var add = (req, res) => {
    var sql = "INSERT INTO `sys`.`img` (`imgname`, `img`) VALUES (?, ?)"
    db.query(sql, [req.body.imgname, req.body.img], (err, data) => {
        if (err) {
            res.send(err);
        }
        res.send(data)
    })
}


//照片添加
var add_a = (req, res) => {
    var sql = "INSERT INTO `sys`.`zp_img` (`zp_img`, `zp_id`) VALUES (?, ?)"
    db.query(sql, [req.body.zp_img, req.body.zp_id], (err, data) => {
        if (err) {
            res.send(err);
        }
        res.send(data)
    })
}

//删除
var del = (req, res) => {
    var sql = "DELETE FROM `sys`.`img` WHERE `id` = ?"
    db.query(sql, [req.body.id], (err, data) => {
        if (err) {
            res.send(err);
        }
        res.send(data)
    })
}

//删除照片
var del_a = (req, res) => {
    var sql = "DELETE FROM `sys`.`zp_img` WHERE `id` = ?"
    db.query(sql, [req.body.id], (err, data) => {
        if (err) {
            res.send(err);
        }
        res.send(data)
    })
}

// 修改
var put = (req, res) => {
    var sql = "UPDATE `sys`.`img` SET `imgname` = ? , `img` = ? WHERE `id` = ?"
    db.query(sql, [req.body.imgname, req.body.img, req.body.id], (err, data) => {
        if (err) {
            res.send("内部服务器错误");
        } else {
            res.send(data);
            console.log(req.body);
        }
    });
}

// 修改照片
var put_a = (req, res) => {
    var sql = "UPDATE `sys`.`zp_img` = ? SET `zp_id` = ? WHERE `id` = ?"
    db.query(sql, [req.body.zp_img, req.body.zp_id, req.body.id], (err, data) => {
        if (err) {
            res.send("内部服务器错误");
        } else {
            res.send(data);
            console.log(req.body);
        }
    });
}




// 搜索
var search = (req, res) => {
    var sql = "SELECT * FROM `sys`.`img` WHERE `id` = ? LIMIT 0,1000"
    db.query(sql, [req.body.id], (err, data) => {
        if (err) {
            res.send("内部服务器错误");
        } else {
            res.send(data);
            console.log(req.body);
        }
    });
}

// 照片搜索
var search_a = (req, res) => {
    var sql = "SELECT * FROM `sys`.`zp_img` WHERE `id` = ? LIMIT 0,1000"
    db.query(sql, [req.body.id], (err, data) => {
        if (err) {
            res.send("内部服务器错误");
        } else {
            res.send(data);
            console.log(req.body);
        }
    });
}




// 相册
var photo = (req, res) => {
    var sql = "SELECT * FROM `sys`.`zp_img` WHERE `zp_id` = ? LIMIT 0,1000"
    db.query(sql, [req.body.id], (err, data) => {
        if (err) {
            res.send("内部服务器错误");
        } else {
            res.send(data);
            console.log(req.body);
        }
    });
}





router.get("/list", list)

router.post("/add", add)
router.post("/add_a", add_a)


router.post("/del", del)
router.post("/del_a", del_a)



router.post("/put", put)
router.post("/put_a", put_a)


router.post("/search", search)
router.post("/search_a", search_a)


router.post("/photo", photo)


router.post("/login", login)
router.post("/dl", dl)







module.exports = router